// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import QMLFile 1.0

AndroidItem {
    id: _root

    property variant paths: []

    property int loadedCount: 0 //ロードした枚数を数える
    property int pictCount: 0   //画像の枚数

    property int sequenceId: 0      //0ロゴ , 1アニメ
    property int sequenceCount: 0   //シーケンス中のカウンタ

    property bool loaded: false                 //ロード完了
    property alias running: _timer.running      //動いてるか

    visible: false//_timer.running
    color: "#000000"

    function start(){
        sequenceId = 0;
        sequenceCount = 0;
        if(_bootanimation.count > 0 && loaded === true){
            if(_timer.running === true){
                _timer.restart();
            }else{
                _timer.start();
            }
        }
    }
    function stop(){
        sequenceId = 0;
        sequenceCount = 0;
        _timer.stop();
    }

    function equalsPaths(dest_paths){
        var ret = true;
        if(paths.length !== dest_paths.length){
            ret = false;
        }else{
            for(var i=0; i<paths.length; i++){
                if(paths[i] !== dest_paths[i]){
                    ret = false;
                }
            }
        }
        return ret;
    }

    //指定されたディレクトリ内のファイルを開く
    function getFileList(path){
        var files = [];
        var list = _file.getEntryList(path, QMLFile.Files, ["*.jpg", "*.jpeg", "*.png"]);
        //内容を登録
        for(var i=0; i<list.length; i++){
            if(list[i] === "." || list[i] === ".."){
            }else{
                files.push(path + list[i]);
            }
        }
        return files;
    }

    onPathsChanged: {
        var files = [];
        var i=0;

        //ファイル一覧を作成
        for(i=0; i<paths.length; i++){
            if(paths[i].length > 0){
                files = files.concat(getFileList(paths[i]));
            }
        }

        //クリア
        loadedCount = 0;
        pictCount = files.length;
        _bootanimation.model.clear();
        //実際に登録
        for(i=0; i<files.length; i++){
            _bootanimation.model.append({"_item": files[i]});
        }
    }

    onLoadedCountChanged: {
        if(loadedCount >= pictCount){
            loaded = true;
        }else{
            loaded = false;
        }
    }

    //アニメーション
    Row{
        id: _area
        x:0
        y:0
        visible: (sequenceId === 1)
        Repeater{
            id: _bootanimation
            model:ListModel{}
            Image{
                source: _item
                width: _root.width
                height: _root.height
                smooth: true
                fillMode: Image.PreserveAspectFit
                onStatusChanged: {
                    if(status === Image.Ready){
                        loadedCount++;
                    }
                }
            }
        }
    }
    //起動ロゴ
    Rectangle{
        id: _bootLogo
        anchors.fill: parent
        color: "#000000"
        visible: (sequenceId === 0)
        Image{
            anchors.centerIn: parent
            width: 288 * viewScale
            height: 118 * viewScale
            source: "images/hint_google.png"
        }
        Image{
            anchors.horizontalCenter: parent.horizontalCenter
            y: 1050 * viewScale
            width: 216 * viewScale
            height: 216 * viewScale
            source: "images/ic_lockscreen_unlock_normal.png"
        }
    }

    //制御
    Timer{
        id: _timer
        running: false
        interval: 66
        repeat: true
        onRunningChanged: {
            _root.visible = running;
        }
        onTriggered:{
            if(sequenceId === 0){
                //ロゴ
                sequenceCount++;
                if(sequenceCount > 33){
                    sequenceId = 1;
                    sequenceCount = 0;
                }
            }else{
                //アニメ
                if((_area.x - _root.width + _area.width) <= 0){
                    _area.x = 0;
                    sequenceCount++;
                }else{
                    _area.x -= _root.width;
                }
                if(sequenceCount >= 1){
                    _root.stop();
                }
            }

        }
    }

}
